/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserLogin.java
 *
 * Created on 2012-6-4, 21:24:07
 */

package icexam.user;

import icexam.ICExamView;
import icexam.dao.PSWDao;
import icexam.util.PropertyUtil;
import icexam.util.PswUtil;

/**
 *
 * @author Ethan
 */
public class AdminLogin extends javax.swing.JPanel {

    /** Creates new form UserLogin */
    public AdminLogin() {
        initComponents();
    }

    private ICExamView mainView;
    public AdminLogin(ICExamView aThis) {
        this();
        mainView = aThis;
    }
    public void initRoleSelect(){
        jComboBox1.setSelectedIndex(1);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        usernameT = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        passwordT = new javax.swing.JPasswordField();
        jPanel7 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        loginB = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(600, 900));
        setMinimumSize(new java.awt.Dimension(350, 35));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(350, 200));
        setLayout(new java.awt.BorderLayout(5, 5));

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(150, 200));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 341, Short.MAX_VALUE)
        );

        add(jPanel1, java.awt.BorderLayout.EAST);

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        add(jPanel3, java.awt.BorderLayout.PAGE_START);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(150, 200));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 341, Short.MAX_VALUE)
        );

        add(jPanel2, java.awt.BorderLayout.WEST);

        jPanel4.setMaximumSize(new java.awt.Dimension(350, 32767));
        jPanel4.setMinimumSize(new java.awt.Dimension(300, 35));
        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setPreferredSize(new java.awt.Dimension(330, 150));

        jPanel5.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(icexam.ICExamApp.class).getContext().getResourceMap(AdminLogin.class);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel5.add(jLabel2);

        usernameT.setEditable(false);
        usernameT.setText(resourceMap.getString("usernameT.text")); // NOI18N
        usernameT.setName("usernameT"); // NOI18N
        usernameT.setPreferredSize(new java.awt.Dimension(200, 21));
        jPanel5.add(usernameT);

        jPanel4.add(jPanel5);

        jPanel6.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel6.add(jLabel3);

        passwordT.setText(resourceMap.getString("passwordT.text")); // NOI18N
        passwordT.setName("passwordT"); // NOI18N
        passwordT.setPreferredSize(new java.awt.Dimension(200, 21));
        jPanel6.add(passwordT);

        jPanel4.add(jPanel6);

        jPanel7.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel7.add(jLabel4);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "考生", "管理员" }));
        jComboBox1.setSelectedIndex(1);
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.setPreferredSize(new java.awt.Dimension(200, 21));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel7.add(jComboBox1);

        jPanel4.add(jPanel7);

        jPanel8.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel8.setName("jPanel8"); // NOI18N
        jPanel8.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel5.setName("jLabel5"); // NOI18N
        jLabel5.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel8.add(jLabel5);

        loginB.setText(resourceMap.getString("loginB.text")); // NOI18N
        loginB.setName("loginB"); // NOI18N
        loginB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBActionPerformed(evt);
            }
        });
        jPanel8.add(loginB);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jPanel8.add(jButton2);

        jPanel4.add(jPanel8);

        add(jPanel4, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        if(jComboBox1.getSelectedIndex()==1){
            mainView.goToAdminLogin();
        }else{
            mainView.goToUserLogin();
        }
}//GEN-LAST:event_jComboBox1ActionPerformed

    private void loginBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBActionPerformed
        
        UserBean user = new UserBean();
        user.setUsername("admin");

        String tmp = this.passwordT.getText();
        PSWDao pdao = new PSWDao();        
        if(tmp==null||tmp.equals("")||!pdao.checkPSW(tmp)){
            javax.swing.JOptionPane.showMessageDialog(this, "密码错误！");
            return;
        }
        user.setPassword(tmp);
        user.setAdmin(true);
        this.mainView.setUser(user);
        this.mainView.goToUserMain(new AdminMainP(this.mainView));
}//GEN-LAST:event_loginBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JButton loginB;
    private javax.swing.JPasswordField passwordT;
    private javax.swing.JTextField usernameT;
    // End of variables declaration//GEN-END:variables

}
